package javaapplication1;
import com.jpackages.jtray.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;



/**
 * Example code using JTray
 * Copyright (c) 2001-2007
 * Company: VersaEdge Software, LLC
 */

public class Example1 extends JFrame {
  // handle to the JTrayIcon displayed
  JTrayIcon jTrayIcon;

  // define a popup mssenu
  JPopupMenu jPopupMenu = new JPopupMenu();
  JMenuItem jMenuItemHideFrame = new JMenuItem();
  JMenuItem jMenuItemShowFrame = new JMenuItem();
  JMenuItem jMenuItemClose = new JMenuItem();
  JMenuItem jMenuItemAbout = new JMenuItem();

  // define a slider
  JSlider slider = new JSlider();

  static Image loadImage(String path) {
	  return (new ImageIcon(path)).getImage();
  }
  
  // define some images 
  Image imageAbout = loadImage("example/image/About24.gif");
  Image imageStop = loadImage("example/image/Stop24.gif");

  // define Example1 Frame GUI elements
  BorderLayout borderLayout1 = new BorderLayout();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea jTextArea1 = new JTextArea();

  public Example1() {
    // initialize GUI components
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }

    try {
      // create an instance of a JTrayIcon which uses image\ani_01.gif as the icon to display
      jTrayIcon = new JTrayIcon("image\\ani_01.gif", ImageFileType.JAVA_SUPPORTED);
      // specify the tool tip text
      jTrayIcon.setToolTipText("JTray Example1");
      // set the right click target to the popup menu after the jPopupMenu and its components have been completely defined
      jTrayIcon.setRightClickTarget(jPopupMenu);
      // set the left click target to the slider
      jTrayIcon.setLeftClickTarget(slider);
      // display this JTrayIcon
      jTrayIcon.show();
    }
    catch (JTrayLibraryLoadFailedException e) {
      System.out.println(e);
      System.exit(0);
    }
  }

  //Main Method
  public static void main(String[] args) {
    Example1 frame = new Example1();

    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.setVisible(false);
  }

  //GUI initialization
  private void jbInit() throws Exception {
    this.setTitle("JTray Example1");
    this.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        this_windowClosing(e);
      }
    });
    this.setSize(300, 200);
    this.getContentPane().setLayout(borderLayout1);
    jTextArea1.setEnabled(false);
    jTextArea1.setDisabledTextColor(Color.darkGray);
    String textMessage = "An icon has been added to the system tray.  Right and left click on it to see example functionality.\n\n"
                       + "If using the trial edition of JTray, an additional demo icon is also displayed.";
    jTextArea1.setText(textMessage);
    jTextArea1.setLineWrap(true);
    jTextArea1.setWrapStyleWord(true);
    jMenuItemHideFrame.setText("Hide Frame");
    jMenuItemHideFrame.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemHideFrame_actionPerformed(e);
      }
    });
    jMenuItemShowFrame.setText("Show Frame");
    jMenuItemShowFrame.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemShowFrame_actionPerformed(e);
      }
    });
    jMenuItemClose.setText("Close");
    jMenuItemClose.setIcon(new ImageIcon(imageStop));
    jMenuItemClose.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemClose_actionPerformed(e);
      }
    });
    jMenuItemAbout.setText("About");
    jMenuItemAbout.setIcon(new ImageIcon(imageAbout));
    jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemAbout_actionPerformed(e);
      }
    });
    this.getContentPane().add(jScrollPane1,  BorderLayout.CENTER);
    jScrollPane1.getViewport().add(jTextArea1, null);
    jPopupMenu.add(jMenuItemHideFrame);
    jPopupMenu.add(jMenuItemShowFrame);
    jPopupMenu.addSeparator();
    jPopupMenu.add(jMenuItemAbout);
    jPopupMenu.add(jMenuItemClose);
  }

  // this method is called when the application should close
  void closeApp() {
    // check if null since it could be null if a JTrayLibraryLoadFailedException is thrown
    if (jTrayIcon != null) {
      // no longer display this icon
      jTrayIcon.hide();
    }
    System.exit(0);
  }

  void jMenuItemHideFrame_actionPerformed(ActionEvent e) {
    this.setVisible(false);
  }

  void jMenuItemShowFrame_actionPerformed(ActionEvent e) {
    this.setVisible(true);
  }

  void jMenuItemClose_actionPerformed(ActionEvent e) {
    closeApp();
  }

  void this_windowClosing(WindowEvent e) {
    closeApp();
  }

  // display About info when about is clicked
  void jMenuItemAbout_actionPerformed(ActionEvent e) {
    // need to disable targets before showing a modal dialog box
    jTrayIcon.disableTargets();
    JOptionPane.showMessageDialog(null, "JTray is a VersaEdge Software, LLC product.  Visit www.jpackages.com for more information.");
    // enable targets after showing a modal dialog box
    jTrayIcon.enableTargets();
  }
}